<?php
# ***** BEGIN LICENSE BLOCK *****
# This file is part of "myWiWall".
# Copyright (c) 2007-2008 CRP Henri Tudor and contributors.
# All rights reserved.
#
# "myWiWall" is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# 
# "myWiWall" is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with "myWiWall"; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# ***** END LICENSE BLOCK *****


/**
 * The Admin module unites the actions relevant to the Administration section
 * of the MyWiWall portal.
 *
 * @author Laurent Haan <laurent.haan@gmail.com>
 * @author J�r�me Bogaerts <jerome.bogaerts@gmail.com>
 * @package mywiwall.actions
 */
class Admin
{
	/**
	 * This action is the default action of the Admin module. It should be triggered
	 * when the user agent request requests to enter the administration panel.
	 */
	public function index()
	{
		// Session message handling.
		if (isset($_SESSION['message']))
		{
			$message = $_SESSION['message'];
			$isError = $_SESSION['isError'];
			
			// We consider message and isError session variables as flash variables.
			unset($_SESSION['message']);
			unset($_SESSION['isError']);
		}
	
		if (!(Auth :: isAuth() && (Auth :: isAdmin() || Auth::isGod()))) 
			// forward to the login script
			DefaultFC::redirection('users/index?ref=admin');
		else
		{
			// Determine if the view must allow the user to manage widgets.
			$widgetManagement = false;
			if (Auth::isAdmin() || Auth::isGod())
				$widgetManagement = true;
			
			// View preparation.
			$categories = $this->buildCategoryList();
			$installedWidgets = Widgets::retrieveWidgetList(Auth::getUserId(), 
															Auth::getUserLevel(),
															'raw',
															true);
			$partial = 'widgets';
															
			require (DefaultFC::getView('admin.tpl'));
		}
	}
	
	/**
	 * This action should be triggered through a classic HTTP POST request in order
	 * to trigger the visibility change in the portal for a given widget.
	 * 
	 * (Developper's note : If a widget is visible, users will be able to add it
	 * in their widget wall.)
	 *
	 * @param string $widget The widget's identifier you need to change the visibilty.
	 */
	public function toggleVisibility($widget)
	{
		// Security check.
		if (!Auth::isAuth() && (Auth::isAdmin() || Auth::isGod()))
			DefaultFC::redirection('users/index?ref=admin');	
		
		try
		{
			// We toggle the visibility of the requested widget through the model.	
			Widgets::toggleVisibility($widget);
			
			$_SESSION['isError'] = false;
			$_SESSION['message'] = __("The visibility of the widget has been successfuly changed.");
		}
		catch (Exception $e)
		{
			$_SESSION['isError'] = true;
			$_SESSION['message'] = __("An error occured when changing the widget visibility.");
		}
		
		DefaultFC::redirection('admin/index');
	}
	
	/**
	 * This action should be triggered through a HTTP POST request in order to
	 * install a widget.
	 * 
	 * @param integory $category The category's identifier to which the installed widget must be added.
	 */
	public function install($category)
	{
		// Security check.
		if (!Auth::isAuth() && (Auth::isAdmin() || Auth::isGod()))
			DefaultFC::redirection('users/index?ref=admin');
		
		try
		{
			// Action : Install the widget.
			$widgetName = Widgets::install($category,
										   Auth::getUserId(),
										   $_FILES['widget']['name'], 
										   $_FILES['widget']['tmp_name']);
			
			$_SESSION['isError'] = false;
			$_SESSION['message'] = sprintf(__("Widget '%s' has been successfully installed."), $widgetName);	
		}
		catch (Exception $e)
		{
			$_SESSION['isError'] = true;
			$_SESSION['message'] = __("An error occured during widget installation.");
		}
	
		DefaultFC::redirection('admin/index');
	}
	
	/**
	 * The Edit action of the Admin module should be triggered through an HTTP POST request
	 * in order to edit a widget that is already installed in the widget repository.
	 *
	 * @param integer $category The category's identifier in which the updated widget will be assigned.
	 * @param string $widget The widget's identifier relevant to the widget to update.
	 */
	public function edit($category, $widget)
	{
		// Security check.
		if (!Auth::isAuth() && (Auth::isAdmin() || Auth::isGod()))
			DefaultFC::redirection('users/index?ref=admin');

		try
		{
			// When editing, two cases may occur :
			// 1. Both widget file (xml manifest or zip archive) and category
			//    are provided.
			// 2. Only the category is provided. Dev note: the $_FILES global has
			//    a strange behaviour when something is not provided... for instance
			//    if a file named 'widgetFile' was not uploaded, the $_FILES['widgetFile']['name']
			//    value will not be empty, or null, but filled with an empty string oO !
			$widgetName = null;
			
			if ($_FILES['widgetFile']['name'] == '')
			{
				// Case 1.
				$widgetName = Widgets::edit($widget,
											$category);
			}
			else
			{
				// Case 2.
				$widgetName = Widgets::edit($widget, 
											$category, 
											$_FILES['widgetFile']['name'],
											$_FILES['widgetFile']['tmp_name']);	
			}
			
			// If we are here, no exceptions was thrown by Widgets::edit so we assume
			// as successful widget edition.
			$_SESSION['isError'] = false;
			$_SESSION['message'] = sprintf(__("Widget '%s' has been successfully updated."), $widgetName);
		}
		catch (Exception $e)
		{
			$_SESSION['isError'] = true;
			$_SESSION['message'] = __("The widget could not be updated.");
		}
		
		DefaultFC::redirection('admin/index');
	}
	
	/**
	 * The Delete action of the Admin module should be triggered through an HTTP POST request in order to
	 * uninstall a widget that was installed in the widget repository.
	 *
	 * @param string $widget The identifier of the widget that must uninstalled from the widget repository.
	 */
	public function delete($widget)
	{
		// Security check.
		if (!Auth::isAuth() && (Auth::isAdmin() || Auth::isGod()))
			DefaultFC::redirection('users/index?ref=admin');	
			
		// Action
		Widgets::uninstall($widget);
														
		$_SESSION['isError'] = false;
		$_SESSION['message'] = __("The widget has been deleted successfully.");

		DefaultFC::redirection('admin/index');
	}
	
	private function buildCategoryList()
	{
		return Categories::retrieveCategories('raw');
	}
}
?>